package com.baizhi.service.impl;

import com.baizhi.dao.UserDAO;
import com.baizhi.entity.SexCount;
import com.baizhi.entity.User;
import com.baizhi.service.UserService;
import com.baizhi.utils.ThreadPollUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ThreadPoolExecutor;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserDAO userDAO;

    @Override
    @Transactional(readOnly = true)
    public PageInfo<User> selectPaeg(String telphone, int page, int limit) {
        PageHelper.startPage(page,limit);
        PageInfo info = new PageInfo(userDAO.select(telphone));
        return info;
    }

    @Transactional
    @Override
    public void status(Integer id, Integer status) {
        userDAO.status(id,status);
    }

    @Override
    @Transactional
    public void insert(User u) {
        userDAO.insert(u);
    }

    @Override
    public List<SexCount> selectSexCount() {
        return userDAO.selectSexCount();
    }

    @Override
    public List<Map> selectProvinceCount() {
        return userDAO.selectProvinceCount();
    }

    @Override
    public List<SexCount> selectRegisterCount(Integer min, Integer max) {
        List<SexCount> sexCounts = new ArrayList<>();
        SexCount sexCount = new SexCount();
        int o = userDAO.selectRegisterCount(0, 7);
        sexCount.setSex("7天之内");
        sexCount.setCount(o);
        sexCounts.add(sexCount);
        int i = userDAO.selectRegisterCount(7, 14);
        SexCount s1 = new SexCount();
        s1.setSex("7-14天之内");
        s1.setCount(i);
        sexCounts.add(s1);
        int i1 = userDAO.selectRegisterCount(14, 21);
        SexCount s2 = new SexCount();
        s2.setSex("14-21天之内");
        s2.setCount(i1);
        sexCounts.add(s2);
        return sexCounts;
    }

    @Override
    public Map selectAllCount(){
        Map map = new HashMap();
        CountDownLatch countDownLatch = new CountDownLatch(3);
        ThreadPoolExecutor executor = ThreadPollUtil.executor;
        executor.submit(new Runnable() {
            @Override
            public void run() {
                List<SexCount> counts = userDAO.selectSexCount();
                map.put("count",counts);
                countDownLatch.countDown();
            }
        });
        executor.submit(new Runnable() {
            @Override
            public void run() {
                List<SexCount> sexCounts = new ArrayList<>();
                SexCount sexCount = new SexCount();
                int o = userDAO.selectRegisterCount(0, 7);
                sexCount.setSex("7天之内");
                sexCount.setCount(o);
                sexCounts.add(sexCount);
                int a = userDAO.selectRegisterCount(7, 14);
                SexCount s1 = new SexCount();
                s1.setSex("7-14天之内");
                s1.setCount(a);
                sexCounts.add(s1);
                int i1 = userDAO.selectRegisterCount(14, 21);
                SexCount s2 = new SexCount();
                s2.setSex("14-21天之内");
                s2.setCount(i1);
                sexCounts.add(s2);
                map.put("register",sexCounts);
                countDownLatch.countDown();
            }
        });
        executor.submit(new Runnable() {
            @Override
            public void run() {
                List<Map> maps = userDAO.selectProvinceCount();
                map.put("province",maps);
                countDownLatch.countDown();
            }
        });
        //通过计数器让主线程等待
        try {
            countDownLatch.await();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return map;
    }

    @Override
    public Map selectNo() {
        Map map = new HashMap();
//        性别
        List<SexCount> counts = userDAO.selectSexCount();
        map.put("count",counts);
//        注册量
        List<SexCount> sexCounts = new ArrayList<>();
        SexCount sexCount = new SexCount();
        int o = userDAO.selectRegisterCount(0, 7);
        sexCount.setSex("7天之内");
        sexCount.setCount(o);
        sexCounts.add(sexCount);
        int i = userDAO.selectRegisterCount(7, 14);
        SexCount s1 = new SexCount();
        s1.setSex("7-14天之内");
        s1.setCount(i);
        sexCounts.add(s1);
        int i1 = userDAO.selectRegisterCount(14, 21);
        SexCount s2 = new SexCount();
        s2.setSex("14-21天之内");
        s2.setCount(i1);
        sexCounts.add(s2);

        map.put("register",sexCounts);
//        地区
        List<Map> maps = userDAO.selectProvinceCount();

        map.put("province",maps);
        return map;
    }
}
